package ZooManager;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;

/**
* User: James Barker
* Date: 6/08/12
*/
public class AdminUI extends JFrame {
    ArrayList<Cage> cageArray = new ArrayList<Cage>();
    final String[] columnNames = {"Enclosure Name", "Enclosure Number", "Enclosure Size", "Enclosure Type", "Number of Entries", "Lights", "Exhibit Name", "Exhibit Description"};
    JTable table;

    /**
    * Creates the main admin UI window
    */
    public AdminUI() {
        this.setTitle("Zoo Manager v1.0");
        this.setSize(850, 430);
        this.setLocation(250, 100);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setLayout(new BorderLayout());
        showAdminTitlePanel();
        showAdminSecurityPanel();
        showAdminTablePanel();
        this.setVisible(true);

        cageArray = new CageCreator();

        populateTable();
    }

    /**
    * Creates the title panel
    */
    public void showAdminTitlePanel() {
        final JPanel titlePanel = new JPanel();
        final JLabel titleLabel = new JLabel("Zoo Manager v1.0");
        titleLabel.setFont(new Font("Serif", Font.PLAIN, 30));
        titlePanel.add(titleLabel, BorderLayout.CENTER);
        this.add(titlePanel, BorderLayout.NORTH);
    }

    /**
    * Creates the security buttons panel
    */
    public void showAdminSecurityPanel() {
        final JPanel securityPanel = new JPanel();
        JButton addCageButton = new JButton("Add Cage");
        addCageButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                CageAddUI cageAddUI = new CageAddUI();
                if (!cageAddUI.isCancelled()) {
                    cageArray.add(cageAddUI.getNewCage());
                    cageAddUI.dispose();
                    populateTable();
                }
            }
        });
        securityPanel.add(addCageButton);

        JButton secureCagesButton = new JButton("Secure Cages");
        secureCagesButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                for (Cage cage : cageArray) {
                    cage.closeAllEntries();
                    cage.setCageUnoccupied();
                }
                JOptionPane.showMessageDialog(null, "All cages have been secured.");
            }
        });
        securityPanel.add(secureCagesButton);

        JButton turnLightsOnButton = new JButton("Turn on Cage Lights");
        turnLightsOnButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                for (Cage cage : cageArray) {
                    cage.turnLightsOn();
                }
                populateTable();
                JOptionPane.showMessageDialog(null, "All cage lights have been switched on.");
            }
        });
        securityPanel.add(turnLightsOnButton);

        JButton turnLightsOffButton = new JButton("Turn off Cage Lights");
        turnLightsOffButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                for (Cage cage : cageArray) {
                    cage.turnLightsOff();
                }
                populateTable();
                JOptionPane.showMessageDialog(null, "All cage lights have been switched off.");
            }
        });
        securityPanel.add(turnLightsOffButton);

        this.add(securityPanel, BorderLayout.SOUTH);
    }

    /**
    * Creates the table panel
    */
    public void showAdminTablePanel() {
        final JPanel tablePanel = new JPanel(new FlowLayout());
        table = new JTable();

        table.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2) {
                    int row = table.getSelectedRow();
                    new CageViewUI(cageArray.get(row));
                    populateTable();
                }
            }
            @Override
            public void mousePressed(MouseEvent e) {}
            @Override
            public void mouseReleased(MouseEvent e) {
                if (e.getButton() == MouseEvent.BUTTON3) {
                    int row = table.rowAtPoint(e.getPoint());
                    table.setRowSelectionInterval(row, row);
                    JPopupMenu popup = createPopupMenu(table.getSelectedRow());
                    popup.show(table, e.getX(), e.getY());
                }
            }
            @Override
            public void mouseEntered(MouseEvent e) {}
            @Override
            public void mouseExited(MouseEvent e) {}
        });
        JScrollPane scrollPane = new JScrollPane(table);
        scrollPane.setPreferredSize(new Dimension(800, 300));
        tablePanel.add(scrollPane);
        this.add(tablePanel, BorderLayout.CENTER);
    }

    /**
    * Called when we need to populate the table
    */
    public void populateTable() {
        List<String[]> list = new ArrayList<String[]>();
        for (Cage c : cageArray) {
            String lightState;
            if (c.isLightsOn()) {
                lightState = "On";
            } else {
                lightState = "Off";
            }

            String[] newRow = { c.getEnclosureName(), String.valueOf(c.getEnclosureNumber()), String.valueOf(c.getEnclosureSize()), c.getEnclosureType(), String.valueOf(c.getNoOfEntries()), lightState, c.getExhibitName(), c.getExhibitDesc() };
            list.add(newRow);
        }
        String[][] data = new String[list.size()][];
        list.toArray(data);

        DefaultTableModel model = new DefaultTableModel(data, columnNames){
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        table.setRowSelectionAllowed(true);
        table.setColumnSelectionAllowed(false);
        table.setModel(model);
    }

    /**
    * Creates a popup menu that is triggered when right clicking the table rows
    */
    public JPopupMenu createPopupMenu(final int rowId) {
        JPopupMenu p = new JPopupMenu();
        JMenuItem mi;

        mi = new JMenuItem("View");
        mi.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new CageViewUI(cageArray.get(rowId));
            }
        });
        p.add(mi);

        mi = new JMenuItem("Edit");
        mi.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
            CageEditUI cageEditUI = new CageEditUI(cageArray.get(rowId));
            cageArray.set(rowId,  cageEditUI.getCage());
            populateTable();
            }
        });
        p.add(mi);

        mi = new JMenuItem("Delete");
        mi.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                cageArray.remove(rowId);
                populateTable();
            }
        });
        p.add(mi);

        return p;
    }
}